<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 9:47
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Session;

class BaseController extends Controller
{
    public function getImages($image)
    {
        return response()->file(base_path('resources/assets/images/home/') . $image);
    }

    /**
     *用户上级查询
     * @param $id 用户id
     * @param $select  查询数据
     * @return mixed
     */
    public function superior($id, $select='*')
    {
        if(is_array($select)){ //查询数据值
            array_walk($select,function (&$s, $k, $prefix = 'T2.') {
                    $s = str_pad($s, strlen($prefix) + strlen($s), $prefix, STR_PAD_LEFT);
                }
            );
            $select=implode(',',$select);
        }
        $sql = "SELECT DISTINCT ".$select."
        FROM
            (
                SELECT
                    @r AS _uid,
                    (
                        SELECT
                            @r := referrer -- 上级id
                        FROM
                            bl_members
                        WHERE
                            uid = _uid
                    ) AS parent_id,
                    @l := @l + 1 AS lvl
                FROM
                    (SELECT @r := ".$id.", @l := 0) vars,
                    bl_members h
                WHERE
                    @r <> 0
            ) T1
        JOIN bl_members T2 ON T1._uid = T2.uid
        WHERE
            T2.uid != ".$id." -- 用户id
        ORDER BY
        T1.lvl ASC";
        $data = DB::select($sql);
        return array_map('get_object_vars', $data);
    }
}